<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
use app\app\util\ShareUtils;

class ShareController extends BaseController{

//	public function ShareDetail()
//	{
//		$id = input('id');
//		$share = Db::name('share')->alias('a')->join('user b','a.user_id=b.id','left')->field('a.id,a.create_time,b.nickname')->where('a.id','eq',$id)->find();
//		//查询出已点赞的人
//		$data = ShareUtils::getUserShareClickId();
//	}

    /**
     *分享
     */
    public function share()
    {
        $click_user_id = OnlineUtils::getUserIdBySid();
        //时间查询
        $today = date('Y-m-d',time());
        $start = $today." 00:00:01";
        $end   = $today." 23:59:59";
        $start = strtotime($start);
        $end   = strtotime($end);
        $click         = Db::name('share')->where('create_time','between',[$start,$end])->where('user_id','eq',input('post.user_id'))->where('click_user_id','eq',$click_user_id)->find();
        if( $click ) return ft(402,'今日已为他助力');
        $user_id       = input('post.user_id');
        if($user_id == $click_user_id) return ft(402,'无法为自己助力');
        $click         = array('user_id'=>$user_id,'click_user_id'=>$click_user_id,'create_time'=>time());
        Db::name('share')->insert($click);
        //贡献值+1
        Db::query('update ft_user set contribute = contribute + 1 where id='.$user_id);
        //为对方+1
        $number = 1;
        ShareUtils::getUserCont($user_id,$number);
        return ft(200,'success');
    }

    /**
     * 贡献榜
     * @return array
     */
    public function ContList()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        //先为自己添加redis贡献值
        $user = Db::name('user')->where('id','eq',$user_id)->find();
        ShareUtils::foundUserCont($user_id,$user['contribute']);
        $start = 0;
        $end   = 50;
        $list  = ShareUtils::getContRank($start,$end);
        // return ft(200);
        //查询出信息
        foreach($list as $k => $v){
            $star = Db::name('user')->field('id,nickname,avatar_url,contribute')->where('id','eq',intval($v))->find();
            $arr[] = $star;
        }
        return ft(200,'success',['data'=>$arr]);
    }


    /**
     * 助力记录（列表）
     * @return array
     */
    public function ShowShare()
    {
        $user_id = input('get.user_id');    //分享用户id
        $user = Db::name('user')->field('id,nickname,avatar_url,contribute')->find($user_id);   //用户信息
        $click = Db::name('share')->alias('a')->join('user b','a.click_user_id=b.id','left')->field('a.id,a.create_time,b.nickname,b.avatar_url')->where('user_id','eq',$user_id)->order('create_time')->select();
        foreach ($click as $k => $v){
            $click[$k]['create_time'] = date('Y-m-s H:i:s',$v['create_time']);
        }
        return ft(200,'success',['user'=>$user,'click'=>$click]);
    }
   
}